package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.Spu;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    private final SpuService spuService;

    private final SpuDetailService detailService;

    private final SkuService skuService;

    public GoodsController(SpuService SpuService, SpuDetailService detailService, SkuService skuService) {
        this.spuService = SpuService;
        this.detailService = detailService;
        this.skuService = skuService;
    }

    /**
     * 分页查询spu
     * @param brandId  品牌id
     * @param categoryId 分类id
     * @param id   商品spu的 id
     * @param page  当前页码
     * @param rows   每页大小
     * @param saleable  是否上架
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>>querySpuByPage(
            @RequestParam(value = "brandId",required = false) Long brandId,
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "id",required = false) Long id,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable){
            return ResponseEntity.ok(spuService.querySpuByPage(brandId,categoryId,id,page,rows,saleable));
    }

    /**
     * 新增商品
     * @param spuDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void>addGoods(@RequestBody SpuDTO spuDTO){
        spuService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**
     * 商品上下架
     * @param spuId
     * @param saleable
     * @return
     */
    @PutMapping("/saleable")
    public ResponseEntity<Void> saleable (
            @RequestParam("id") Long spuId,
            @RequestParam("saleable")Boolean saleable){
        spuService.saleable(spuId,saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    /**
     * 根据id查询商品
     */
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO>queryGoodsById(@PathVariable("id") Long id){
            return ResponseEntity.ok(spuService.queryGoodsById(id));
    }
    /**
     * 根据id查询SpuDetail
     * 根据id查询detail
     * @param id 商品id
     * @return 商品的Detail
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id){
        return ResponseEntity.ok(new SpuDetailDTO(detailService.getById(id)));
    }

    /**
     *根据spuId查询sku集合
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>>querySKUsBySpuId(@RequestParam("id") Long id){
        List<Sku> spu_id = skuService.query().eq("spu_id", id).list();
        List<SkuDTO> skuDTOS = SkuDTO.convertEntityList(spu_id);
        return ResponseEntity.ok(skuDTOS);
    }

    /**
     * 根据id集合查询SKU集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>>querySKUsByIds(@RequestParam("ids") List<Long> ids){
        List<Sku> skus = skuService.listByIds(ids);
        List<SkuDTO> skuDTOS = SkuDTO.convertEntityList(skus);
        return ResponseEntity.ok(skuDTOS);
    }

    /**
     * 根据Id查询商品Spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO>querySpuById(@PathVariable("id") Long id){
        Spu spuServiceById = spuService.getById(id);
        SpuDTO spuDTO = new SpuDTO(spuServiceById);
        return ResponseEntity.ok(spuDTO);
    }

    /**
     * 商品修改
     * @param id
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>>querySpecParam(
            @RequestParam("id") Long id,
            @RequestParam(value = "searching",required = false) Boolean searching){
            return ResponseEntity.ok(detailService.querySpecParam(id,searching));
    }
}